//
//  FS3DScrollView.swift
//  联付商圈
//
//  Created by zhangqi on 2017/7/14.
//  Copyright © 2017年 lianfu. All rights reserved.
//

import UIKit


protocol clickImgDelegate{
    func clickImg(_ index: NSInteger)
}

class FS3DScrollView: UIView {

    var currentIndex: Int? = 0
    
    var imageView: UIImageView?
    
    var imageArr: [String]?
    
    var delegate: clickImgDelegate?
    
    var timer: Timer?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func show3DView(_ imgArr: [String]){
        imageArr = imgArr
        //定义图片控件
        imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: mainScreenW + 20, height: 58))
        imageView!.contentMode = UIViewContentMode.scaleToFill
        
        imageView!.image = UIImage(named: imageArr![0])//默认图片
        imageView!.isUserInteractionEnabled = true
        isUserInteractionEnabled = true
        addSubview(imageView!)
        imageView!.tag = 0
        
        //let doubleTap = UITapGestureRecognizer(target: self, action: #selector(FS3DScrollView.doDoubleTap(_:)))
        //默认点击第一张
        //imageView!.addGestureRecognizer(doubleTap)

        
        //添加手势
//        let leftSwipeGesture = UISwipeGestureRecognizer(target: self ,action: #selector(FS3DScrollView.leftSwipe(_:)))
//        leftSwipeGesture.direction = UISwipeGestureRecognizerDirection.up//left
//        
//        addGestureRecognizer(leftSwipeGesture)
        
//        let rightSwipeGesture = UISwipeGestureRecognizer(target: self ,action: #selector(FS3DScrollView.rightSwipe(_:)))
//        rightSwipeGesture.direction=UISwipeGestureRecognizerDirection.down//right
//        addGestureRecognizer(rightSwipeGesture)

    }
    
    
    //向左滑动浏览下一张图片
//    func leftSwipe(_ gesture: UISwipeGestureRecognizer){
//        self.transitionAnimation(true)
//    }
    
    //向右滑动浏览上一张图片
    func rightSwipe(){
        self.transitionAnimation(false)
    }
    
    // -  转场动画
    func transitionAnimation(_ isNext: Bool){
    //1.创建转场动画对象
        let transition = CATransition()
    
    //2.设置动画类型,注意对于苹果官方没公开的动画类型只能使用字符串，并没有对应的常量定义
        transition.type = "cube"
    
    //设置子类型
        if (isNext) {
            transition.subtype=kCATransitionFromTop//kCATransitionFromRight;
        }else{
            transition.subtype=kCATransitionFromBottom//kCATransitionFromLeft;
        }
    //设置动画时常
        transition.duration = 1.0
    
    //3.设置转场后的新视图添加转场动画
        imageView!.image = getImage(isNext)
        imageView!.layer.add(transition, forKey: "KCTransitionAnimation")
    }
    
    //取得当前图片
    func getImage(_ isNext: Bool) -> UIImage {
        if (isNext) {
            currentIndex = (currentIndex! + 1) % imageArr!.count
        }else{
            currentIndex = (currentIndex! - 1 + imageArr!.count) % (Int)(imageArr!.count)
        }
        let imageName: String = imageArr![currentIndex!]
    
    
        imageView!.tag = currentIndex!
        
        let doubleTap = UITapGestureRecognizer(target:self ,action: #selector(FS3DScrollView.doDoubleTap(_:)))
    
        imageView!.addGestureRecognizer(doubleTap)
    
    
        return UIImage(named: imageName)!
    }
    
    func doDoubleTap(_ gesture: UITapGestureRecognizer)
    {
        delegate?.clickImg((gesture.view?.tag)!)
    }
    
    
    /// 开启倒计时
    func startTimer() {
        if timer == nil {
            timer = Timer(timeInterval: 3.0, target: self, selector: #selector(self.rightSwipe), userInfo: nil, repeats: true)
            RunLoop.main.add(timer!, forMode: RunLoopMode.commonModes)
        }
    }
    
    /// 停止倒计时
    func stopTimer() {
        if timer != nil {
            timer?.invalidate()
            timer = nil
        }
        
    }
    
    /// 计时器响应方法
//    func timerAction() {
//        // 更新位置, 更换图片
//        
//        UIView.animate(withDuration: 3.0, animations: {
//            self.FS3DScrollView.setContentOffset(CGPoint(x: self.bounds.size.width * 2, y: 0), animated: true)
//            
//        }, completion: nil)
//        
//    }

}
